module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 先删除依赖表
    await queryInterface.dropTable('table_buttons');
    await queryInterface.dropTable('table_columns');
    // 删除旧表
    await queryInterface.dropTable('table_configs');
    
    // 创建新的动态表格配置表
    await queryInterface.createTable('table_configs', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      tenant_id: {
        type: Sequelize.UUID,
        allowNull: true,
        comment: '租户ID',
        references: {
          model: 'tenants',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      code: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: '表格配置唯一标识',
      },
      name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '表格名称',
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '表格描述',
      },
      table_name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '对应的数据库表名',
      },
      api_path: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'API路径前缀',
      },
      fields: {
        type: Sequelize.JSON,
        allowNull: false,
        comment: '字段配置JSON',
      },
      actions: {
        type: Sequelize.JSON,
        allowNull: true,
        defaultValue: {
          create: true,
          update: true,
          delete: true,
          batchDelete: false,
          export: false,
          import: false,
        },
        comment: '操作权限配置',
      },
      table_props: {
        type: Sequelize.JSON,
        allowNull: true,
        defaultValue: {
          bordered: false,
          size: 'middle',
          pagination: { pageSize: 10 },
        },
        comment: '表格属性配置',
      },
      permissions: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '权限配置',
      },
      status: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: '状态',
      },
      created_by: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'users',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
      },
      updated_by: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'users',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });

    // 添加索引
    await queryInterface.addIndex('table_configs', ['tenant_id']);
    await queryInterface.addIndex('table_configs', ['code']);
    await queryInterface.addIndex('table_configs', ['status']);
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable('table_configs');
  },
};
